<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter="dataFormSubmit()" label-width="100px">
    <el-form-item label="主键id" prop="id" v-if="dataForm.id">
        <el-input v-model="dataForm.id" placeholder="主键id" disabled></el-input>
    </el-form-item>
    <el-form-item label="微信账户" prop="weixinAccount">
        <el-input v-model="dataForm.weixinAccount" placeholder="微信账户"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
        <el-input v-model="dataForm.avatar" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickName">
        <el-input v-model="dataForm.nickName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择">
          <el-option v-for="item in sexOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="email地址" prop="email">
        <el-input v-model="dataForm.email" placeholder="email地址"></el-input>
    </el-form-item>
    <el-form-item label="公司名称" prop="company">
        <el-input v-model="dataForm.company" placeholder="公司名称"></el-input>
    </el-form-item>
    <el-form-item label="部门id" prop="deptId">
        <el-input v-model="dataForm.deptId" placeholder="部门id"></el-input>
    </el-form-item>
    <el-form-item label="unionId" prop="unionId">
        <el-input v-model="dataForm.unionId" placeholder="unionId"></el-input>
    </el-form-item>
    <el-form-item label="openId" prop="openId">
        <el-input v-model="dataForm.openId" placeholder="openId" disabled></el-input>
    </el-form-item>
    <el-form-item label="认证状态" prop="recognizeStatus">
        <el-select v-model="dataForm.recognizeStatus" placeholder="请选择">
          <el-option v-for="item in recognizeStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
    </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/smart/oiluser'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        sexOptions: [
          {
            value: '男',
            label: '男'
          },
          {
            value: '女',
            label: '女'
          }
        ],  
        recognizeStatusOptions: [
          {
            value: 0,
            label: '未认证'
          },
          {
            value: 1,
            label: '认证中'
          },
          {
            value: 2,
            label: '认证通过'
          },
          {
            value: 3,
            label: '认证不通过'
          }
        ],
        dataForm: {
          id: '',
          weixinAccount: '',
          phone: '',
          avatar: '',
          userName: '',
          nickName: '',
          sex: '',
          address: '',
          email: '',
          company: '',
          deptId: '',
          unionId: '',
          openId: '',
          recognizeStatus: '',
          createBy: '',
          updateBy: '',
          createTime: '',
          updateTime: '',
        },
        dataRule: {
          phone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],

          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],

          nickName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],

          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],

          company: [
            { required: true, message: '公司名称不能为空', trigger: 'blur' }
          ],

          openId: [
            { required: true, message: 'openId不能为空', trigger: 'blur' }
          ],

          recognizeStatus: [
            { required: true, message: '认证状态不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.id = ''
            this.dataForm.createBy = ''
            this.dataForm.updateBy = ''
            this.dataForm.createTime = ''
            this.dataForm.updateTime = ''
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
